<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios from '../plugins/axios'
import { ElMessage } from 'element-plus'
import LessonDetails from '../components/LessonDetails.vue'
import { ArrowLeft, Share } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const lesson = ref(null)

const handleShare = async () => {
  try {
    const url = window.location.href
    await navigator.clipboard.writeText(url)
    ElMessage.success('链接已复制到剪贴板')
  } catch (err) {
    console.error('Failed to copy:', err)
    ElMessage.error('复制链接失败')
  }
}

const fetchLessonDetail = async () => {
  try {
    const response = await axios.get(`/lesson/${route.params.id}`)
    if (response.data.code === 1) {
      lesson.value = response.data.data
    } else {
      ElMessage.error(response.data.msg || '获取SOP详情失败')
    }
  } catch (error) {
    console.error('获取SOP详情失败:', error)
    ElMessage.error('获取SOP详情失败')
  }
}

const handleBack = () => {
  router.back()
}

onMounted(() => {
  fetchLessonDetail()
})
</script>

<template>
  <div class="lesson-detail-view">
    <div class="detail-header">
      <div class="header-left">
        <el-button @click="handleBack" type="text">
          <el-icon><ArrowLeft /></el-icon> 返回列表
        </el-button>
      </div>
      <div class="header-right">
        <el-button 
          type="primary" 
          @click="handleShare"
          class="share-button"
        >
          <el-icon><Share /></el-icon>
          分享
        </el-button>
      </div>
    </div>
    <LessonDetails v-if="lesson" :lesson="lesson" />
  </div>
</template>

<style scoped>
.lesson-detail-view {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.detail-header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.share-button {
  display: flex;
  align-items: center;
  gap: 5px;
}

.detail-header h2 {
  font-size: 18px;
  font-weight: bold;
  margin-left: 10px;
}
</style> 